package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.RightsDao;
import cn.ym.mall.entity.Rights;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class RightsDaoImpl implements RightsDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 查询所有权限
     * @return
     */
    @Override
    public List<Rights> getAll() throws SQLException {
        String sql="select rigid, rigname from ym_rights;";
        List<Rights> rightsList=null;
        try{
            rightsList= queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Rights>(Rights.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return rightsList;
    }

    /**
     * 添加新权限
     *
     * @param rights
     * @return
     */
    @Override
    public int add(Rights rights) throws SQLException {
        String sql="insert into ym_rights(rigname) value(?);";
        Object[]  arr={rights.getRigname()};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据权限id删除权限
     *
     * @param rgid
     * @return
     */
    @Override
    public int delete(Integer rgid) throws SQLException{
        String sql="delete from ym_rights where rigid=?;";
        Object[]  arr={rgid};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 更新现有权限
     *
     * @param rights
     * @return
     */
    @Override
    public int update(Rights rights) throws SQLException{
        String sql="update ym_rights set rigname=? where rigid=?;";
        Object[]  arr={rights.getRigname(),rights.getRigid()};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 获得权限总数总数
     * @return
     */
    @Override
    public long getCount() throws SQLException {
        long count = 0;
        try{
            String sql = "select count(0) as count from ym_rights;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<>());
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 根据角色编号查权限
     * @param rigid
     * @return
     */
    @Override
    public Rights getRightsById(Integer rigid) throws SQLException{
        Rights rights=null;
        try{
            String sql="select rigid, rigname from ym_rights where rigid=?;";
            rights= queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Rights>(Rights.class),rigid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rights;
    }
}
